package eu.dicode.analysis.demonstrator.model;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Provides the web frontend with all known queries that have been tracked by us.
 *
 * Implementation is not thread safe. Can be used in a threaded context only if
 * initialized at the very beginning and re-used but left stable there-after.
 * Concurrent modification and usage not recommended.
 */
public class HomeCommand {
  private final Collection<String> allKnownQueries;

  private volatile String selectedQuery;

  public HomeCommand() {
    this.allKnownQueries = new ArrayList<String>();
  }

  /**
   * Returns a safe copy of the collection of all query strings known to the system.
   * Modification of the returned collection does NOT lead to modification of the
   * internal state of this object.
   * */
  public Collection<String> getAllKnownQueries() {
    Collection<String> value = new ArrayList<String>();
    value.addAll(this.allKnownQueries);
    return value;
  }

  /**
   * Resets the internal collection of all known queries to the content passed in
   * as parameter. For safety reasons call this method only for initialization and
   * leave it alone thereafter if multiple threads are using this command.
   * */
  public void setAllKnownQueries(final Collection<String> allKnownQueries) {
    this.allKnownQueries.clear();
    this.allKnownQueries.addAll(allKnownQueries);
  }

  public String getSelectedQuery() {
    return this.selectedQuery;
  }

  public void setSelectedQuery(final String selectedQuery) {
    this.selectedQuery = selectedQuery;
  }
}
